package com.example.cuibo.loginvc;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Map;

public class LoginActivity extends AppCompatActivity {

    private EditText ed_uerName;
    private EditText ed_uerPwd;
    private CheckBox box;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.longiinlinlayout);

        // 找到控件
        ed_uerName = (EditText)findViewById(R.id.ed_userName);
        ed_uerPwd = (EditText)findViewById(R.id.ed_userPwd);
        box = (CheckBox)findViewById(R.id.cb_isRemeberPwd);

        // 读取info.txt信息\
        Map<String,String> map = UserInfoUtils.readInfo(LoginActivity.this);
        if (map != null){
            // 拿到name 和 pwd 取出来
            String account = map.get("name");
            String pwd = map.get("pwd");
            ed_uerName.setText(account);
            ed_uerPwd.setText(pwd);
        }
    }

    // 按钮的点击事件
    public void login(View v){
        String acount = ed_uerName.getText().toString();
        String pws = ed_uerPwd.getText().toString();
        if (TextUtils.isEmpty(acount) || TextUtils.isEmpty(pws)){

            Toast.makeText(LoginActivity.this,"用户名密码不能为空", 1).show();
        }else {
            if (box.isChecked()){
                // 保存用户名密码
               boolean result = UserInfoUtils.saveInfo(LoginActivity.this,acount,pws);
                if (result){
                    Toast.makeText(LoginActivity.this,"保存成功",1).show();
                }else {
                    Toast.makeText(LoginActivity.this,"保存失败",1).show();
                }

            }else {
                Toast.makeText(LoginActivity.this,"请勾选cb", 1).show();
            }
            // 进行登录
            System.out.print("连接服务器");
        }
    }
}
